<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>promise封装AJAX</title>
</head>
<body>
  <div onclick="handle()">发起请求</div>
</body>
<script>
  function handle(){
    //1.创建 xhr 实例对象
    // let xhr = new XMLHttpRequest();
    //2.初始化 xhr 实例对象
    // xhr.open('GET','https://tenapi.cn/wyyinfo/?id=400162138');
    //3.发送
    // xhr.send();
    //4.接收返回值
    // xhr.onreadystatechange = function(){
    //   console.log('xhr.readyState--',xhr.readyState)
    //   if(xhr.readyState === 4){
    //     if(xhr.status >= 200 && xhr.status < 300){
    //       console.log(xhr.response)
    //     } else {
    //       console.log(xhr.status)
    //     }
    //   }
    // }

    let p = new Promise((resolve,reject)=>{
      let xhr = new XMLHttpRequest();
      xhr.open('GET','https://tenapi.cn/wyyinfo/?id=400162138');
      xhr.responseType = 'json';
      xhr.send();
      xhr.onreadystatechange = function(){
        if(xhr.readyState === 4){
          if(xhr.status >= 200 && xhr.status < 300){
            resolve(xhr.response)
          } else {
            reject(xhr.status)
          }
        }
      }
    })
    p.then(value=>{
      console.log(value);
    },error=>{
      console.warn(error);
    })
  }
</script>
</html>